/*
// 加法
function abc (arr)
{
    var he = 0;
    for (let i = 0; i < arr.length; i++) 
    {
         he += arr[i];
    }
    return he;
}
console.log(abc([1,2,3,4]));

// 查找索引

  function suoyin (arr,item)
  {
      if(arr.indexOf(item)>0)
      {
          return arr.indexOf(item)
      }
      return -1;
  }
  console.log(suoyin(["Banana","Orange","Apple","Mango","Banana","Orange","Apple"],"Mango"));

// 删除索引

function shanchu (arr,item)
{   
    var ad = [];
    ad.forEach(function(edc){
        if(item != edc)
        {
            ad.push(edc);        }
    })
    return ad;
}
console.log(shanchu([1,2,3,4,5,1],1));

function  remove (arr,item){
    var aa =[];
    arr.forEach(function(arritem) {
        if(item != arritem){
          aa.push(arritem);
        }
    })
    return aa;
}

console.log(remove([1,2,3,4,2],2));
*/
var
    form = $('#test-form'),
    langs = form.find('[name=lang]'),
    selectAll = form.find('label.selectAll :checkbox'),
    selectAllLabel = form.find('label.selectAll span.selectAll'),
    deselectAllLabel = form.find('label.selectAll span.deselectAll'),
    invertSelect = form.find('a.invertSelect');

// 重置初始化状态:
form.find('*').show().off();
form.find(':checkbox').prop('checked', false).off();
deselectAllLabel.hide();
// 拦截form提交事件:
form.off().submit(function (e) {
    e.preventDefault();
    alert(form.serialize());
});
// TODO:绑定事件:
 selectAll.change(function(){
    if(this.checked){
      langs.prop('checked',true);
      deselectAllLabel.show();
      selectAllLabel.hide();
    }else{
      langs.prop('checked',false);
      deselectAllLabel.hide();
      selectAllLabel.show();
    }
   })
 
   invertSelect.click(function(){
    langs.each(function(){
      if(this.checked){
        $(this).prop('checked',false);
      }else{
        this.checked = true;
      }
    })
   })
  
   var count = 0,
       len = langs.length;
   langs.click(function(){
    count = 0;
    langs.each(function(){
      if(this.checked)
        count++;
    })
    if(count == len){
      selectAll.prop('checked',true);
      deselectAllLabel.show();
      selectAllLabel.hide();
    }else{
        selectAllLabel.show();
        deselectAllLabel.hide();
        selectAll.prop('checked',false);
    }
   });


